package com.waimai.app.qa.support;

import static org.monte.media.FormatKeys.EncodingKey;
import static org.monte.media.FormatKeys.FrameRateKey;
import static org.monte.media.FormatKeys.KeyFrameIntervalKey;
import static org.monte.media.FormatKeys.MIME_AVI;
import static org.monte.media.FormatKeys.MIME_QUICKTIME;
import static org.monte.media.FormatKeys.MediaTypeKey;
import static org.monte.media.FormatKeys.MimeTypeKey;
import static org.monte.media.VideoFormatKeys.CompressorNameKey;
import static org.monte.media.VideoFormatKeys.DepthKey;
import static org.monte.media.VideoFormatKeys.ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE;
import static org.monte.media.VideoFormatKeys.QualityKey;

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;

import org.monte.media.Format;
import org.monte.media.math.Rational;
import org.monte.screenrecorder.ScreenRecorder;

import com.waimai.app.qa.webdriver.AppDriverPlus;

/**
 * ScreenRecoder,封装屏幕录像方法
 * @author AiHui
 */
public class ScreenRecoder {
	
  public ScreenRecorder screenRecorder;
  
  //此工具生成的视频路径, 默认为系统的C:\Users\XX\Videos, 若要自定义视频路径，需继承ScreenRecorder类，重写createMovieFile方法
  //详细参考:http://web.archiveorange.com/archive/v/yXYbUHmR24y4eGZbEKDj
  //http://www.randelshofer.ch/monte/javadoc/org/monte/screenrecorder/ScreenRecorder.html
  //默认支持的视频文件格式为AVI和QUICKTIME格式,其中AVI相对较大
  
  /**
   * 开始屏幕录像
   * @author AiHui
   */
  public void startRecording() throws Exception {
      GraphicsConfiguration gc = GraphicsEnvironment
              .getLocalGraphicsEnvironment()
              .getDefaultScreenDevice()
              .getDefaultConfiguration();
      this.screenRecorder = new ScreenRecorder(gc,
			  new Format(MediaTypeKey, org.monte.media.FormatKeys.MediaType.FILE, MimeTypeKey, MIME_AVI),
    		  //new Format(MediaTypeKey, org.monte.media.FormatKeys.MediaType.FILE, MimeTypeKey,MIME_QUICKTIME),
			  new Format(MediaTypeKey, org.monte.media.FormatKeys.MediaType.VIDEO, EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,DepthKey, (int)24, FrameRateKey, Rational.valueOf(15),QualityKey, 1.0f,KeyFrameIntervalKey, (int) (15 * 60)),
			  new Format(MediaTypeKey, org.monte.media.FormatKeys.MediaType.VIDEO, EncodingKey,"black",FrameRateKey, Rational.valueOf(30)),
			  null);
      this.screenRecorder.start();
  }
  
  /**
   * 停止屏幕录像
   * @author aihui
   */
  public void stopRecording() throws Exception {
	  GraphicsConfiguration gc = GraphicsEnvironment
              .getLocalGraphicsEnvironment()
              .getDefaultScreenDevice()
              .getDefaultConfiguration();
      
	  this.screenRecorder = new ScreenRecorder(gc,
			  //new Format(MediaTypeKey, org.monte.media.FormatKeys.MediaType.FILE, MimeTypeKey, MIME_AVI),
    		  new Format(MediaTypeKey, org.monte.media.FormatKeys.MediaType.FILE, MimeTypeKey,MIME_QUICKTIME),
			  new Format(MediaTypeKey, org.monte.media.FormatKeys.MediaType.VIDEO, EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,DepthKey, (int)24, FrameRateKey, Rational.valueOf(15),QualityKey, 1.0f,KeyFrameIntervalKey, (int) (15 * 60)),
			  new Format(MediaTypeKey, org.monte.media.FormatKeys.MediaType.VIDEO, EncodingKey,"black",FrameRateKey, Rational.valueOf(30)),
			  null);
            this.screenRecorder.stop();
            Thread.currentThread().sleep(3000);
            
            //获取待拷贝的目标目录
            String dirPath=System.getProperty("user.dir") + "\\" + System.getProperty("VideosLocation");
           
            //获取当前系统信息,拼接当前系统用户的Videos目录(生成的视频文件默认存放在Videos目录) 
            String sourPath= "C:\\Users\\" + System.getProperty("user.name") + "\\Videos";
            
            AppDriverPlus.sourCopyDir(sourPath, dirPath);
    }
}
